<!--商品防伪码-->
<meta charset="utf-8"/>
<div class="row">
  <div class="col-xs-12">
    <div class="row">
      <div class="col-xs-12">
        <div style="height:36px;margin-top:2px">
          <table class="no-border" style="display:inline">
            <tr>
              <td permission='goods_code_btn_listData' style="padding-right:6px;display:none">
                商品名称
              </td>
              <td permission='goods_code_btn_listData' style="display:none">
                <div class="input-group">
                  <span class="input-group-btn">
                    <button data-toggle="dropdown" class="btn btn-primary btn-white dropdown-toggle col-sm-12" style="margin-left:0px">
                      <span id="searchLabel_goods_id">全部商品</span>
                      <i class="ace-icon fa fa-angle-down"></i>
                    </button>
                    <ul class="dropdown-menu col-sm-12" id="ulSearch_goods_id" style="width:512px;min-width:512px"></ul>
                    <input id="key_goods_id" type="hidden"/>
                  </span>
                </div>
              </td>
              <td permission='goods_code_btn_listData' style="padding-right:6px;padding-left:6px;display:none">
                防伪码
              </td>
              <td permission='goods_code_btn_listData' style="display:none">
                <input type="text" id="key_code" class="form-control search-query" placeholder="输入防伪码[区分大小写]" style="width:170px"/>
              </td>
              <td permission='goods_code_btn_listData' style="display:none">
                <div class="input-group">
                  <span class="input-group-btn"><button type="button" class="btn btn-inverse btn-white" id="btnSearch" title="搜索查询"><span class="ace-icon fa fa-search bigger-110"></span>搜索</button></span>
                </div>
              </td>
              <td permission='goods_code_btn_listData' style="display:none" class="hidden-480">
                <div class="input-group">
                  <span class="input-group-btn"><button type="button" class="btn btn-inverse btn-white" id="btnReset" title="重置清空搜索关键字"><span class="ace-icon fa fa-undo bigger-110"></span>重置</button></span>
                </div>
              </td>
              <td permission='goods_code_btn_add' style="display:none">
                <div class="input-group">
                  <span class="input-group-btn"><button type="button" class="btn btn-inverse btn-white" id="btnAdd" title="新增防伪码"><span class="ace-icon fa fa-download bigger-110"></span>防伪码</button></span>
                </div>
              </td>
            </tr>
          </table>
          <div class="hidden-480 pull-right tableTools-container" permission='goods_code_btn_listData' style="display:none;margin-bottom:2px"></div>
        </div>
        <table id="tableListGoodsCode" class="table table-striped table-bordered table-hover">
          <tbody>
          <tr>
            <td style="text-align:center;height:37px">正在加载……</td>
          </tr>
          </tbody>
        </table>
      </div>
    </div>
  </div>
</div>
<div id="div_goods_code_edit" style="display:none;padding-top:2px">
  <form class="form-horizontal" id="form_goods_code_edit" role="form">
    <label class="col-sm-3 control-label no-padding-right"><div class="hr-4">商品名称<span style="color:#f00;display:inline-block;vertical-align:middle;width:auto;height:14px;line-height:18px">*</span></div></label>
    <div class="col-sm-8">
      <div class="hr-4"></div>
      <div class="input-group" title="选择商品">
        <span class="input-group-btn">
          <button data-toggle="dropdown" class="btn btn-primary btn-white dropdown-toggle col-sm-12" style="margin-left:0px">
            <span id="labelDynamic_goods_id">选择商品</span>
            <i class="ace-icon fa fa-angle-down"></i>
          </button>
          <ul class="dropdown-menu col-sm-12" id="ulDynamic_goods_id"></ul>
          <input id="goods_id" maxlength="19" type="hidden"/>
        </span>
      </div>
    </div>
    <label class="col-sm-3 control-label no-padding-right"><div class="hr-4">防伪码数量<span style="color:#f00;display:inline-block;vertical-align:middle;width:auto;height:14px;line-height:18px">*</span></div></label>
    <div class="col-sm-8">
      <div class="hr-4"></div>
      <input type="text" id="total" placeholder="防伪码数量，最大500个。推荐不大于200个" class="form-control" maxlength="10" title="输入本次生成的防伪码数量,最大500个,推荐小于200个,数量越大等待的时间越久" oninput="value=value.replace(/[^\d]/g,'')"/>
    </div>
  </form>
</div>
<div id="divKeyColumns" style="display:none;padding:1px 0px;"></div>
<div style="display:none;">
  <input type="hidden" id="goods_code_btn_listData"/>
  <input type="hidden" id="goods_code_btn_add"/>
  <input type="hidden" id="goods_code_row_queryById"/>
  <input type="hidden" id="goods_code_btn_goods_id"/>
</div>
<script type="text/javascript">
  ;(function($){
    var tableDom = '#tableListGoodsCode';
    var urlRoute = '/goodsCode/';/*请求controller层的url*/
    var getList = urlRoute + 'listData';/*获取数据列表*/
    var urlAdd = urlRoute + 'add';/*添加*/
    var urlQueryDown = urlRoute + 'queryDown';/*查询并下载*/
    var getGoodsList = urlRoute + 'getGoodsList';/*获取字段goods_id的下拉框列动态值选项的数据*/
    var stateOriginal = {};//仅供参考，更新优化-选项1,可参考项目law-frontend的页面serviceAd.html和后端law-backend的ServiceADService.java;记录编辑的值,仅在编辑时可用
    var thisTable = null;
    thisPage = {
      init : function(){
        this.initTable();
        this.createDropdown();
        this.initDom();
        this.addEvent();
      },
      initDom : function(){
      },
      createDropdown : function(){
        layerFn.ajaxGetResult(getGoodsList,{},function(data){
          if(AppKey.code.code200 == data.code){
            winFn.initDropdown(data.data,'#ulDynamic_goods_id','#goods_id','#labelDynamic_goods_id','选择商品');
            winFn.initDropdown(data.data,'#ulSearch_goods_id','#key_goods_id','#searchLabel_goods_id','全部商品');
          }else{
            winFn.initDropdown([],'#ulDynamic_goods_id','#goods_id','#labelDynamic_goods_id',data.msg);
            winFn.initDropdown([],'#ulSearch_goods_id','#key_goods_id','#searchLabel_goods_id',data.msg);
          }
        });
      },
      addEvent : function(){
        $('#btnAdd').on('click', function(){
          thisPage.addGoodsCode();
        });
        //搜索按钮
        $('#btnSearch').on('click', function(){
          thisPage.search(true);
        });
        //重置按钮
        $('#btnReset').on('click', function(){
          $('#key_code').val('');
          $('#searchLabel_goods_id').text('全部商品');
        });
        //按钮组-自定义显示列
        $('#iconColumn').parent().parent().on('click', function(){
          pageDataTable.columnCustom(thisTable, tableDom,'#divKeyColumns');
        });
        //按钮组-搜索
        $('#iconRefresh').parent().parent().on('click', function(){
          thisPage.search(true);
        });
      },
      /**获取已选中的主键集合,用法:var kids = thisPage.getKeyIds();if(kids == null || kids == ''){layerFn.alert('请选择要操作的数据', AppKey.code.code199);return;}*/
      getKeyIds : function(){
        var arrays = thisTable.rows({selected : true}).data();//获取已勾选的数据行
        if(arrays.length == 0){
          return null;
        }
        var kids = '';
        for(var i = 0; i < arrays.length; i++){
          var value = arrays[i].kid;
          if(value != ''){
            if(kids.length > 0) kids += ",";
            kids += value;
          }
        }
        return kids;
      },
      initTable : function(){
        var optsVisible = getOptsVisible('goods_code_row');//确定是否要显示操作选项,若是多个按钮权限时以逗号,隔开
        var optsBatchBtn = getOptsVisible('goods_code_btn_delByKeys');//是否有批量操作的权限,若是多个按钮权限时以逗号,隔开
        var aoColumns = [];
        aoColumns.push({
          data : "contract_name",
          sortable : false,
          title : "商品名称"
        });
        aoColumns.push({
          data : "real_name",
          sortable : false,
          title : "首次查看人",
          render : function(value){
            return value || '';
          }
        });
        aoColumns.push({
          data : "check_time",
          sortable : false,
          title : "首次查看时间",
          width: "121px",
          render : function(value){
            if(value){
              return winFn.formatDate(value,true);
            }else{
              return "";
            }
          }
        });
        aoColumns.push({
          data : "code",
          sortable : false,
          width : "250px",
          title : "防伪码"
        });
        aoColumns.push({
          data : "check_total",
          sortable : false,
          width : "110px",
          title : "查看次数",
          render : function(value){
            if(value){
              return value;
            }else{
              return "0";
            }
          }
        });
        aoColumns.push({
          data : "add_date",
          sortable : false,
          title : "生成时间",
          width: "121px",
          render : function(value){
            if(value){
              return winFn.formatDate(value,true);
            }else{
              return "";
            }
          }
        });
        if(optsVisible){
          aoColumns.push({
            data : "_kid_",
            width : "75px",
            class : 'text-center',
            title : "<label style='color:#000' title='单行操作'>操作选项</label>",
            sortable : false
          });
        }
        var columnDefs = [];
        if(optsVisible){//是否来显示某列
          columnDefs.push({
            targets : -1,
            render : function(value,type,row,meta){
              return "<a title='单独下载防伪码' href='javascript:thisPage.rowDown("+ meta.row +");' style='outline:none;text-decoration:none;color:#3b8cff;display:none' permission='goods_code_row_down'>下载</a>";
            }
          });
        }
        columnDefs.push({
          targets : optsBatchBtn ? [0,1,2,3,4,5] : [0,1,2,3,4,5],
          render : function(value,type,row,meta){
            return pageDataTable.formatColumn(value);
          }
        });
        columnDefs.push({
          visible : false,//列的隐藏显示
          targets : []//指定列索引
        });
        thisTable = pageDataTable.initDataTable({
          tableDom : tableDom,
          sAjaxSource : getList,
          fnServerParams : function(params){
            params.push(
              {"name":"goods_id","value":winFn.getDomValue('#key_goods_id')},
              {"name":"code","value":winFn.getDomValue('#key_code')}
            );
          },
          aoColumns : aoColumns,
          columnDefs : columnDefs,
          complete : function(settings,data){},//仅执行一次
          footer : function(data,start,end,thLength){},//每次都会执行
          selected : function(selected){
            if(selected){
              //$('#btnDel').removeAttr('disabled');//默认在该button添加属性disabled="disabled"
            }else{
              //$('#btnDel').attr('disabled','disabled');
            }
          }
        });
        pageDataTable.buttons(thisTable);
        pageDataTable.action(thisTable);
        pageDataTable.select(thisTable, tableDom);
        pageDataTable.tooltip();
        $('.dataTables_scroll .dataTables_scrollBody').removeAttr('style');//解决dataTable表头下拉框被tbody遮挡
      },
      //是否是搜索,是则搜索时则返回到第一页
      search : function(search){
        if(verifyFn.inputCheck('#goods_code_btn_listData','没有操作权限')) return;
        $(tableDom + '_wrapper table input[type=checkbox]').prop('checked',false);//此行不需要,因为搜索时表格已重新渲染
        //thisTable.draw();保留
        if(search){
          $(tableDom).DataTable().ajax.reload();//编辑时成功会重载所有接口数据，返回到第一页
        }else{
          $(tableDom).DataTable().ajax.reload(null,false);//编辑时成功重新加载表数据(分页页码保留);
        }
      },
      //需要时或为空时才去请求,减轻服务器查询,仅适用于不需要分页的数据,用法:thisPage.getGoods_codes(function(list){});参考项目 wine-frontend/src/main/resources/static/activity.html的getGoods方法
      getGoods_codes : function(callback){
        if(listGoods_code == null){//定义全局的 var listGoods_code = null;//新增或编辑时所需的数据
          layerFn.queryGetHintResult(urlQueryGoods_code,{},function(data){
            if(AppKey.code.code200 == data.code){
              listGoods_code = data.data;
              callback(listGoods_code);
            }else{
              callback();//注意判断 if(listGoods_code != null){}为空,可参考 liquor-console/src/main/resources/static/goods.html
            }
          });
        }else{
          callback(listGoods_code);
        }
      },
      rowDown : function(rowIndex){
        authHandler('goods_code_row_down',function(){
          var row = thisTable.row(rowIndex).data();
          layerFn.handleTop('请耐心等待,处理完成将会自动下载!',AppKey.code.code200);
          winFn.getPageHashToken(urlQueryDown+'?kid='+row.kid+'&name='+row.contract_name);
        },function(){
          layerFn.alert('暂无操作权限',AppKey.code.code199);
        });
      },
      resetForm : function(){
        winFn.clearFormData('#form_goods_code_edit');
        winFn.addDropdown('#labelDynamic_goods_id','选择商品');
      },
      addGoodsCode : function(){
        if(verifyFn.inputNull('#goods_code_btn_add','没有添加权限')) return;
        self.layer_index = layerFn.addOrEdit('生成商品防伪码','#div_goods_code_edit', ['450px','310px'], function(layerIndex,layero){
          if(verifyFn.inputNull('#goods_id','请选择要生成防伪码的商品')) return;
          if(verifyFn.inputNull('#total','请输入生成防伪码数量')) return;
          var total = winFn.getDomValue('#total',true);
          if(parseInt(total) > 500){
            layerFn.alert('防伪码数量不宜大于500个', AppKey.code.code199);
            return;
          }
          var params = {
            goods_id : winFn.getDomValue('#goods_id',true),
            total : total,
            target : 1
          };
          layerFn.submit(urlAdd,params,function(data){
            thisPage.complete(data,layerIndex,true);
              var result = data.data;
              setTimeout(()=>{
                winFn.downloadFile(urlAdd+'?target=2'+'&folder='+result.folder);
              },100);
          });
        });
        $('#layui-layer' + self.layer_index + ' a.layui-layer-btn0').text('生成');
        thisPage.resetForm();
      },
      //移除不需要对比的key(此方法可有可无)
      editJson : function(data,keys){
        if(keys){
          for(var h = 0; h < keys.length; h++){
            delete data[keys[h]];//删除key元素
          }
        }
        stateOriginal = data;
      },
      //查看隐藏部分是全部内容,用法 render : function(value,type,row,meta){return thisPage.rowColumnView(value);}
      rowColumnView : function(value){
        if(value == null || value == undefined || value == '') return '';
        value = typeof value === 'string' ? value.replace(/</g, '&lt;').replace(/"/g, '&quot;') : value;//如果提示SyntaxError: expected expression, got '>'错误则注释掉即可
        var len = value.length;
        if(len > 30){
          return "<a href='javascript:layerFn.viewValue(\""+value+"\",\"430px\");' title='点击查看详情' style='outline:none;text-decoration:none;'>"+value.substring(0,30)+"…</a>";
        }else{
          return value;
        }
      },
      complete : function(data,index,search){
        try{
          if(index){
            layerFn.closeIndex(index);
          }
          if(search){
            thisPage.search();
          }
          if(data){
            layerFn.handleResult(data.msg,data.code);
          }
        }catch(e){}
      }
    };
    thisPage.init();
    $('script').remove();
  })(jQuery);
</script>